package MercuryTour;

import java.io.File;
import java.io.IOException;
import java.util.concurrent.TimeUnit;

import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;


import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.PageFactory;

public class TestEndToEndProcess {

	/**
	 * @param args
	 * @throws IOException 
	 * @throws BiffException 
	 */
	public static void main(String[] args) throws BiffException, IOException {
		
		//Define spreadsheets
		File file = new File ("c:/test.xls");
		Workbook workbook = Workbook.getWorkbook(file);
		Sheet logininfosheet = workbook.getSheet("LoginInfo");
		Sheet findaflightinfosheet = workbook.getSheet("FindAFlightInfo");
		Sheet selectaflightsheet = workbook.getSheet("SelectAFlight");
		Sheet bookaflightsheet = workbook.getSheet("BookInfo");
		
		//Define arrays to store values read from spreadsheets 
		int sizeOfArray = logininfosheet.getColumns();
		String[] loginInfo = new String[sizeOfArray];
		
		int sizeOfFlightInfoSheet = findaflightinfosheet.getColumns();
		String[] flightInfo = new String[sizeOfFlightInfoSheet];
		
		int sizeOfSelectFlightPage = selectaflightsheet.getColumns();
		String[] selectFlight = new String[sizeOfSelectFlightPage];
		
		int sizeOfBookInfoPage = bookaflightsheet.getColumns();
		String[] bookInfo = new String[sizeOfBookInfoPage];
		
		for (int i=0; i< logininfosheet.getRows();i++)
		{
		WebDriver driver = new FirefoxDriver();
		String baseUrl = "http://newtours.demoaut.com/";
		driver.get(baseUrl);
		loginPage loginpage = PageFactory.initElements(driver, loginPage.class);
		
		for (int j=0; j<logininfosheet.getColumns();j++)
		{	
			
			loginInfo[j] = logininfosheet.getCell(j, i).getContents();
	
		}
		loginpage.loginTo(loginInfo);
		
		driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
		
		FindAFlightPage findaflightpage = PageFactory.initElements(driver, FindAFlightPage.class);
		
			for(int l=0;l<findaflightinfosheet.getColumns();l++)
			{
				flightInfo[l] = findaflightinfosheet.getCell(l, i).getContents();
			}
			findaflightpage.assignValuesAsSelection(flightInfo);
			findaflightpage.clickContinueButton();
			
		driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
		
		SelectAFlightPage selectaflightpage = PageFactory.initElements(driver, SelectAFlightPage.class);
		
			for(int k=0;k<selectaflightsheet.getColumns();k++)
			{
				selectFlight[k] = selectaflightsheet.getCell(k, i).getContents();
			}
		selectaflightpage.selectOutAndInFlights(selectFlight);
		selectaflightpage.clickReserveFlightButton();
		
		driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
		
		BookAFlight bookaflight = PageFactory.initElements(driver, BookAFlight.class);
		
		for(int h=0;h<bookaflightsheet.getColumns();h++)
		{
			bookInfo[h] = bookaflightsheet.getCell(h, i).getContents();
		}
		
		bookaflight.assignBookInfo(bookInfo);
		bookaflight.clickPurchaseButton();
		
		driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
		
		ConfirmationPage confirmationpage = PageFactory.initElements(driver, ConfirmationPage.class);
		
		confirmationpage.clickLogoutButton();
		
		driver.close();
		}

	}

}
